<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #template {
            display: none;
        }
    </style>
</head>

<body>
    <ul id="list">
        <li id="template">
            <a href="detail.html"></a>
        </li>
    </ul>
    <script src="jquery.min.js"></script>
    <script>
        //AJAX(HTTP脚本化:用JS代码来控制HTTP协议)
        let http = new XMLHttpRequest()
        http.open('get', "http://127.0.0.1/getNewsList")
        http.send()
        http.onreadystatechange = function() {
            if (http.readyState === 4) {
                // console.log(http.responseText);
                //正解析
                let data = JSON.parse(http.responseText)
                console.log(data);

                let tem = $("#template")
                let list = $("#list")

                data.forEach(item => {
                    let newItem = tem.clone()
                        // attr() 设置/获取一个元素的行内属性的属性值
                    newItem.attr("id", "").find("a").html(item.tltle).attr('href', `detail.html?id=${item.id}`)

                    newItem.appendTo(list)
                })

            }


        }
    </script>

</body>

</html>